Checked.opEquals

Compares this against rhs for equality. If Hook defines hookOpEquals, the function forwards to hook.hookOpEquals(get, rhs). Otherwise, the result of the built-in operation get == rhs is returned.

If U is also an instance of Checked, both hooks (left- and right-hand side) are introspected for the method hookOpEquals. If both define it, priority is given to the left-hand side.

struct Checked(T, Hook = Abort)
bool
opEquals
(
U
this _
)
(
U rhs
)
if (
isIntegral!U ||
isFloatingPoint!U
||
is(U == bool)
||
is(U == Checked!(V, W),
V
W
) &&
is(typeof(this == rhs.payload))
)
if (
isIntegral!T ||
is(T == Checked!(U, H),
U
H
)
)

Examples

1 static struct MyHook
2 {
3     static bool thereWereErrors;
4     static bool hookOpEquals(L, R)(L lhs, R rhs)
5     {
6         if (lhs != rhs) return false;
7         static if (isUnsigned!L && !isUnsigned!R)
8         {
9             if (lhs > 0 && rhs < 0) thereWereErrors = true;
10         }
11         else static if (isUnsigned!R && !isUnsigned!L)
12             if (lhs < 0 && rhs > 0) thereWereErrors = true;
13         // Preserve built-in behavior.
14         return true;
15     }
16 }
17 auto a = checked!MyHook(-42);
18 assert(a == uint(-42));
19 assert(MyHook.thereWereErrors);
20 MyHook.thereWereErrors = false;
21 assert(checked!MyHook(uint(-42)) == -42);
22 assert(MyHook.thereWereErrors);
23 static struct MyHook2
24 {
25     static bool hookOpEquals(L, R)(L lhs, R rhs)
26     {
27         return lhs == rhs;
28     }
29 }
30 MyHook.thereWereErrors = false;
31 assert(checked!MyHook2(uint(-42)) == a);
32 // Hook on left hand side takes precedence, so no errors
33 assert(!MyHook.thereWereErrors);

Meta